package com.jhgsys.internal.hazard.service.impl;

import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.hazard.entity.HazardTypeOperation;
import com.jhgsys.internal.hazard.mapper.HazardTypeOperationMapper;
import com.jhgsys.internal.hazard.service.IHazardTypeOperationService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 隐患类型 Service实现
 *
 * @author wanghw
 * @date 2024-08-29 15:10:21
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardTypeOperationServiceImpl extends ServiceImpl<HazardTypeOperationMapper, HazardTypeOperation> implements IHazardTypeOperationService {

    /**
     * 查询（分页）
     * @author wanghw
     * @date 2024-08-29 15:10:21
     * @param request QueryRequest
     * @param hazardTypeOperation hazardTypeOperation
     * @return IPage<HazardTypeOperation>
    */
    @Override
    public IPage<HazardTypeOperation> findHazardTypeOperationPage(QueryRequest request, HazardTypeOperation hazardTypeOperation) {
        LambdaQueryWrapper<HazardTypeOperation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardTypeOperation::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(hazardTypeOperation.getTypeName())){
            queryWrapper.like(HazardTypeOperation::getTypeName,hazardTypeOperation.getTypeName());
        }
        if(StringUtils.isNotBlank(hazardTypeOperation.getStatus())){
            queryWrapper.eq(HazardTypeOperation::getStatus,hazardTypeOperation.getStatus());
        }
        if(StringUtils.isNotBlank(hazardTypeOperation.getMajorAccidentFlg())){
            queryWrapper.eq(HazardTypeOperation::getMajorAccidentFlg,hazardTypeOperation.getMajorAccidentFlg());
        }
        queryWrapper.orderByAsc(HazardTypeOperation::getSort);
        queryWrapper.orderByDesc(HazardTypeOperation::getCreatedTime);
        Page<HazardTypeOperation> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author wanghw
     * @date 2024-08-29 15:10:21
     * @param hazardTypeOperation hazardTypeOperation
     * @return List<HazardTypeOperation>
     */
    @Override
    public List<HazardTypeOperation> findHazardTypeOperationList(HazardTypeOperation hazardTypeOperation) {
	    LambdaQueryWrapper<HazardTypeOperation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardTypeOperation::getDeletedFlg, 0);
        if(StringUtils.isNotBlank(hazardTypeOperation.getTypeName())){
            queryWrapper.like(HazardTypeOperation::getTypeName,hazardTypeOperation.getTypeName());
        }
        if(StringUtils.isNotBlank(hazardTypeOperation.getStatus())){
            queryWrapper.eq(HazardTypeOperation::getStatus,hazardTypeOperation.getStatus());
        }
        if(StringUtils.isNotBlank(hazardTypeOperation.getMajorAccidentFlg())){
            queryWrapper.eq(HazardTypeOperation::getMajorAccidentFlg,hazardTypeOperation.getMajorAccidentFlg());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author wanghw
    * @date 2024-08-29 15:10:21
    * @param id
    * @return HazardTypeOperation
    */
    @Override
    public HazardTypeOperation findHazardTypeOperationById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author wanghw
     * @date 2024-08-29 15:10:21
     * @param hazardTypeOperation hazardTypeOperation
     */
    @Override
    @Transactional
    public void createHazardTypeOperation(HazardTypeOperation hazardTypeOperation) {
        hazardTypeOperation.setCreatedTime(new Date());
        this.save(hazardTypeOperation);
    }

    /**
     * 修改
     * @author wanghw
     * @date 2024-08-29 15:10:21
     * @param hazardTypeOperation hazardTypeOperation
     */
    @Override
    @Transactional
    public void updateHazardTypeOperation(HazardTypeOperation hazardTypeOperation) {
        hazardTypeOperation.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardTypeOperation);
    }

    /**
     * 逻辑删除
     * @author wanghw
     * @date 2024-08-29 15:10:21
     * @param hazardTypeOperation hazardTypeOperation
     * @param hazardTypeOperationIds hazardTypeOperationIds
     */
    @Override
    @Transactional
    public void deleteHazardTypeOperation(HazardTypeOperation hazardTypeOperation,String hazardTypeOperationIds) {
        List<String> list = Arrays.asList(hazardTypeOperationIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardTypeOperation,new LambdaQueryWrapper<HazardTypeOperation>().in(HazardTypeOperation::getTypeOperationId,list));
	}

    /**
    * 物理删除
    * @author wanghw
    * @date 2024-08-29 15:10:21
    * @param hazardTypeOperationIds hazardTypeOperationIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardTypeOperation(String hazardTypeOperationIds) {
        List<String> list = Arrays.asList(hazardTypeOperationIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardTypeOperation> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardTypeOperation::getTypeOperationId,list);
        this.remove(wapper);
    }
}
